#!/bin/sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
APP_INSTALL_NAME="maxkb"
APP_TARGET_DIR_NAME="maxkb"
echo "lmdprogress:10"

# stop docker container
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/docker_image_util.sh"

gen_image_repo_cpu $_DOCKER_IMAGE_NAME $_DOCKER_IMAGE_NAME_LMD $_DOCKER_IMAGE_TAG

APP_IMAGE_REPO=$APP_IMAGE_REPO_RESULT
echo "docker image repo: "$APP_IMAGE_REPO

docker stop $APP_IMAGE_REPO || true
docker rm $APP_IMAGE_REPO || true

echo "lmdprogress:40"

# remove docker image
docker rmi $APP_IMAGE_REPO


export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH


if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist $CURRENT_APP_PATH"
    echo "lmdprogress:100"
fi

echo "lmdprogress:80"

cd "$CURRENT_APP_PATH"

if [ -d "$CURRENT_APP_PATH" ]; then
    # remove models
        
    if [ "$#" -eq 0 ]; then
        DB_DIR="./postgresql-data"
        echo 'remove postgresql-data'
        rm -rf $DB_DIR

        PYTHON_PACKAGES="./python-packages"
        echo 'remove python-packages'
        rm -rf $PYTHON_PACKAGES

    elif [ "$#" -eq 1 ] && [ "$1" = "keepmodels" ]; then
        #  "keepmodels" : don't delete models
        echo 'keep models'
    fi
fi

echo "lmdprogress:100"
